package com.wx.controller;

import com.example.dao.RecordDao;
import com.example.dao.UserDao;
import com.example.pojo.Record;
import com.example.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.List;

@Controller
public class login {
    @Autowired
    RecordDao recordDao;
    @Autowired
    UserDao userDao;
    int temp=0;
    String name;
    @RequestMapping("/get")
    public void get(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("text/html;charset=utf-8");
        /*设置响应头允许ajax跨域访问*/
        response.setHeader("Access-Control-Allow-Origin", "*");
            response.setHeader("Access-Control-Allow-Origin", "GET,POST");
        /* 星号表示所有的异域请求都可以接受， */
        String userName = request.getParameter("userName");
        name=userName;
        String passWord = request.getParameter("passWord");
        System.out.println("usernameInput:"+ userName+" passwordInput:"+passWord);
        //返回值给微信小程序
        List<User> all = userDao.findAll();
        for (User user : all) {
            System.out.println(user);
        }
        for (User user : all) {
                if (user.getLoginName().equals(userName) && user.getLoginPwd().equals(passWord)) {
                    temp = 1;
                    break;
                } else {
                    temp = 0;
            }
        }
        if (temp==1){
            System.out.println("登录成功");
        }
        else {
            System.out.println("失败");
        }
//        以输入流的形式重写小程序的js文件
        String in="Page({\n" +
                "  data: {\n" +
                "  listData:[\n";
        List<Record> records = recordDao.findByKind(userName,"");
        for (Record record : records) {
            in=in+"  {\"BookName\":"+"\""+record.getName()+"\""+",\"number\":"+"\""+record.getNumber()+"\""+",\"time\":"+"\""+record.getTime()
                    +"\""+"},\n";
        }
        in=in.substring(0,in.length()-1);
        in=in+"  \n  ]\n" +
                "  }\n" +
                " })";
        System.out.println(in);
        BufferedWriter out = new BufferedWriter(new FileWriter("D:\\WeChatProjects\\demp\\pages\\index\\table.js"));
        out.write(in);
        out.close();


        String in1="Page({\n" +
                "  data: {\n" +
                "  listData:[\n";
        List<Record> records1 = recordDao.findByKind(userName,"1");
        for (Record record : records1) {
            in1=in1+"  {\"BookName\":"+"\""+record.getName()+"\""+",\"number\":"+"\""+record.getNumber()+"\""+",\"time\":"+"\""+record.getTime()
                    +"\""+"},\n";
        }
        in1=in1.substring(0,in1.length()-1);
        in1=in1+"  \n  ]\n" +
                "  }\n" +
                " })";
        System.out.println(in1);
        BufferedWriter out1 = new BufferedWriter(new FileWriter("D:\\WeChatProjects\\demp\\pages\\index\\borrow.js"));
        out1.write(in1);
        out1.close();
    }
}
